package cn.nacos.system.controller;

import cn.nacos.common.entity.Result;
import cn.nacos.common.entity.ResultCode;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:ZhouYang
 * @Description:
 * @Date: Created in 2020/8/28 17:12
 * @Modified By：
 */
@RestController
@Log4j2
@RequestMapping("/sys")
public class LoginController {


   @GetMapping("/login")
    public Result login(@RequestParam String userName,@RequestParam String password){

       UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
       //2.获取subject
       Subject subject = SecurityUtils.getSubject();

       //3.调用login方法调用realm完成用户认证
       subject.login(token);
        //4.获取sessionId
       String sessionId = (String) subject.getSession().getId();

       return new Result(ResultCode.SUCCESS,sessionId);
   }


}
